package com.rural.bus;

import org.json.JSONObject;

import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import cn.jpush.android.api.JPushInterface;

import com.rural.bus.model.User;
import com.rural.bus.net.OnNetCallback;
import com.rural.bus.utils.MobileUtil;
import com.rural.bus.utils.ProgressUtil;

public class LoginActivity extends BaseActivity {
	private EditText etPhoneNum, etUserpsw;
	private Button signin;
	private TextView tvForgetPsw;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		initView();
		setListeners();
		
		initData();
	}

    protected void setListeners(){
    	   signin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String phonenum = etPhoneNum.getText().toString();
				String psw = etUserpsw.getText().toString();
				
				boolean isMoblieLogic=MobileUtil.isMobileNO(phonenum);
				if(!isMoblieLogic){
					toast("手机号非法，请输入正确的手机号");
					return;
				}
				login(phonenum, psw);
			}
		});

		mBtRight.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				startActivity(RegisterActivity.class);
				finish();
			}
		});
		
        mImgLeft.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
        
        tvForgetPsw.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
//				startActivity(ResetPswActivity.class);
			}
		});
    }
    
	@Override
	protected void initView() {
		signin = (Button) findViewById(R.id.signin);
		etPhoneNum = (EditText) findViewById(R.id.et_phonenum);
		etUserpsw = (EditText) findViewById(R.id.userpsw);
//		btQQlogin=(Button) findViewById(R.id.bt_qqlogin);
		
		tvForgetPsw=(TextView) findViewById(R.id.tv_forget_psw);
		tvForgetPsw.setText(Html.fromHtml("<u><font color=\"#023cfa\">忘记密码？</font></u>"));
		
		mBtMiddle.setVisibility(View.VISIBLE);
		mBtMiddle.setText("登录");
		mBtMiddle.setTextColor(getResources().getColor(R.color.white));
		
		mImgLeft.setVisibility(View.VISIBLE);
		mImgLeft.setBackgroundResource(R.drawable.back_bg_selector);
		
		mBtRight.setText("注册");
		mBtRight.setVisibility(View.VISIBLE);
		mBtRight.setTextColor(getResources().getColor(R.color.white));
	}

	@Override
	protected void initData() {

	}
	
	/**
	 * 登陆用户
	 */
	private void login(String phonenum,String psw) {
		ProgressUtil.showProgress(this, "");
		String regionId=JPushInterface.getRegistrationID(this);

		final User user=new User();
		user.login(LoginActivity.this,phonenum, psw,regionId, new OnNetCallback() {
			
			@Override
			public void success(JSONObject object) {
				ProgressUtil.closeProgress();
				update(user);
			}
			
			@Override
			public void fail(String error) {
				ProgressUtil.closeProgress();
				toast(error);
			}
		});
	}
	
	/***
	 * 更新用户的regionId
	 */
	private void update(final User user){
		user.update(this,new OnNetCallback() {
			
			@Override
			public void success(JSONObject object) {
				finish();

			}
			
			@Override
			public void fail(String error) {
				toast("更新用户信息失败 "+error);
				finish();

			}
		});
	}
	
	
}
